package com.daxt.service.impl;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.rmi.CORBA.Util;
import javax.servlet.http.HttpServletRequest;

import com.daxt.mapper.sys.ArchivesClassMapper;
import com.daxt.mapper.sys.ArchivesFondsMapper;
import com.daxt.model.dic.DicDocState;
import com.daxt.model.dic.SystemConfigType;
import com.daxt.model.dic.VersionsMarkType;
import com.daxt.model.dto.BaseQueryDto;
import com.daxt.model.dto.Classifys;
import com.daxt.model.search.ArchivesSearch;
import com.daxt.model.service.archives.vo.*;
import com.daxt.service.ArchivesService;
import com.daxt.service.OperationLogService;
import com.daxt.service.UserService;
import com.daxt.utils.ArchivesUtil;
import com.daxt.utils.StringUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
//import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daxt.base.common.util.RedisUtil;
import com.daxt.base.common.util.UserInfoUtil;
import com.daxt.common.result.PageInfo;
import com.daxt.common.result.PageUtil;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.archives.ArchivesBaseMapper;
import com.daxt.mapper.archives.ArchivesDestroyRecordMapper;
import com.daxt.mapper.archives.ArchivesDocExtMapper;
import com.daxt.mapper.archives.ArchivesDocFileMapper;
import com.daxt.mapper.archives.ArchivesDocMapper;
import com.daxt.mapper.archives.ArchivesDocValueMapper;
import com.daxt.mapper.archives.ArchivesTypeDocMapper;
import com.daxt.mapper.sys.ArchivesTypeMapper;
import com.daxt.mapper.sys.UserFondsMapper;
import com.daxt.mapper.use.UseBorrowRecordMapper;
import com.daxt.model.service.archives.result.ArchivesDetails;
import com.daxt.model.service.archives.result.ArchivesDocResult;
import com.daxt.model.service.archives.result.ArchivesList;
import com.daxt.model.service.base.result.UserInfo;
import com.daxt.model.service.use.vo.UseBorrowRecord;
import com.daxt.service.DocService;

import cn.hutool.core.codec.Base64Decoder;

@Service
public class DocServiceImpl extends ServiceImpl<ArchivesDocMapper, ArchivesDoc> implements DocService {

    @Autowired
    private ArchivesBaseMapper archivesBaseMapper;
    @Autowired
    private ArchivesDocValueMapper archivesDocValueMapper;
    @Autowired
    private ArchivesDocMapper archivesDocMapper;
    @Autowired
    private ArchivesDocExtMapper archivesDocExtMapper;
    @Autowired
    private ArchivesTypeDocMapper archivesTypeDocMapper;
    @Autowired
    private ArchivesTypeMapper archivesTypeMapper;
    @Autowired
    private ArchivesFondsMapper archivesFondsMapper;
    @Autowired
    private UseBorrowRecordMapper borrowRecordMapper;
    @Autowired
    private UserFondsMapper userFondsMapper;
    @Autowired
    private ArchivesDocFileMapper archivesDocFileMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ArchivesDestroyRecordMapper archivesDestroyRecordMapper;
    @Autowired
    private UserInfoUtil userInfoUtil;
    @Autowired
    private OperationLogService operationLogService;
    @Autowired
    private ArchivesService archivesService;
    @Autowired
    private ArchivesClassMapper archivesClassMapper;
    @Autowired
    private UserService userService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<String> docSave(ArchivesDoc archivesDoc) {
        try {
            // archivesDoc.setState("0");
            if (StringUtils.isEmpty(archivesDoc.getFileType())) {
                archivesDoc.setFileType("scattered");
            }
            if (archivesDoc.getDossierId() == null) {
                archivesDoc.setDossierId(0L);
            }
//            if (StringUtils.isEmpty(archivesDoc.getDossierId())) {
//                archivesDoc.setDossierId(0L);
//            }


            if (archivesDoc.getDossierId().intValue() > 0) {
                ArchivesDoc archivesDossier = archivesDocMapper.selectById(archivesDoc.getDossierId());
                if (archivesDossier != null) {
                    archivesDoc.setArchivalNum(archivesDossier.getArchivalNum());
                    archivesDoc.setFondsId(archivesDossier.getFondsId());
                    archivesDoc.setTypeNum(archivesDossier.getTypeNum());
                    archivesDoc.setClassNum(archivesDossier.getClassNum());
                    archivesDoc.setClassName(archivesDossier.getClassName());
                    archivesDoc.setArchivalYear(archivesDossier.getArchivalYear());
                    archivesDoc.setArchivesCode(archivesDossier.getArchivesCode());
                    archivesDoc.setCategoryNum(archivesDossier.getCategoryNum());
                    archivesDoc.setDeptId(archivesDossier.getDeptId());
                    archivesDoc.setDeptName(archivesDossier.getDeptName());
                    archivesDoc.setDossierNum(archivesDossier.getDossierNum());
                    archivesDoc.setStoragePeriod(archivesDossier.getStoragePeriod());
                }
            }
            archivesDocMapper.insert(archivesDoc);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "", "");
        }
        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", "");
    }

    //    @Override
//    public Result<ArchivesList> list(Integer pageNum, Integer pageSize, String typeId, String fondsId,
//                                     String dossierId, String condition, String classId, String keywords, String fileType, String deptId,
//                                     String state, String isDelete, String openState) {
//        ArchivesList resultList = new ArchivesList();
////        QueryWrapper<ArchivesType> queryWrapperType = new QueryWrapper<ArchivesType>();
////        queryWrapperType.eq("fondsId", fondsId);
////        queryWrapperType.eq("typeId", typeId);
////        List<ArchivesType> list = archivesTypeMapper.selectList(queryWrapperType);
//
//        //获取门类
//        ArchivesType typeBean = archivesTypeMapper.selectById(typeId);
//
//        if (null != typeBean) {
//
//            QueryWrapper<ArchivesTypeDoc> col = new QueryWrapper<>();
//            String tempFileType1 = "scattered".equals(fileType) ? "doc" : fileType;
//            col.eq("extType", tempFileType1);
//            col.eq("isList", "1");
//            col.eq("typeId", typeBean.getId());
//            col.select("id", "extColumn", "columnComment", "colsize", "sort", "extType", "dataType", "listLength");
//            List<ArchivesTypeDoc> baseCols = archivesTypeDocMapper.selectList(col);
//
//            QueryWrapper<ArchivesTypeDoc> queryCol = new QueryWrapper<>();
//            queryCol.eq("extType", tempFileType1);
//            queryCol.eq("isQuery", "1");
//            queryCol.eq("typeId", typeBean.getId());
//            queryCol.select("extColumn");
//            List<ArchivesTypeDoc> queryCols = archivesTypeDocMapper.selectList(queryCol);
//            List<String> query = new ArrayList<>();
//            for (int i = 0; i < queryCols.size(); i++) {
//                query.add(queryCols.get(i).getExtColumn());
//            }
//
//            Page<ArchivesDoc> page = new Page<ArchivesDoc>(pageNum == null ? 0 : pageNum,
//                    pageSize == null ? 10 : pageSize);
//
//            Map<String, Object> param = new HashMap<String, Object>();
//            param.put("cols", query);
//            param.put("fondsId", fondsId);
//            param.put("typeId", typeId);
//            if (!StringUtils.isEmpty(classId)) {
//                param.put("classNum", classId);
//            }
//            if (!StringUtils.isEmpty(deptId)) {
//                param.put("deptId", deptId);
//            }
//            if (!StringUtils.isEmpty(state))
//                param.put("state", state);
//            param.put("isDelete", isDelete);
//            if (!StringUtils.isEmpty(dossierId)) {
//                param.put("dossierId", dossierId);
//            }
//            List<String> queryCondition = ArchivesUtil.queryCondition(condition);
//            param.put("fileType", fileType);
//            param.put("query", queryCondition);
//            if (!StringUtils.isEmpty(fileType))
//                //孝义排序  前面增加 分类 classNum
//                if (RedisUtil.isAppoint(VersionsMarkType.VERSIONS_3)) {
//                    param.put("sort", "classNum ," + ("dossier".equals(fileType) ? "dossierNum" : "itemNum"));
//
//                } else {
//                    param.put("sort", "dossier".equals(fileType) ? "dossierNum" : "itemNum");
//                }
//            if (!StringUtils.isEmpty(keywords)) {
//                QueryWrapper<ArchivesType> queryWrapperTypeC = new QueryWrapper<ArchivesType>();
//                queryWrapperTypeC.eq("fondsId", fondsId);
//                queryWrapperTypeC.eq("typeNum", typeId);
//                queryWrapperTypeC.select("id");
//                List<ArchivesType> selectList = archivesTypeMapper.selectList(queryWrapperTypeC);
//                if (selectList != null && selectList.size() > 0) {
//                    QueryWrapper<ArchivesTypeDoc> queryWrapperTypeDoc = new QueryWrapper<ArchivesTypeDoc>();
//                    queryWrapperTypeDoc.eq("typeId", selectList.get(0).getId());
//                    queryWrapperTypeDoc.eq("isList", "1");
//                    List<ArchivesTypeDoc> typeDocList = archivesTypeDocMapper.selectList(queryWrapperTypeDoc);
//                    String key = "";
//                    for (ArchivesTypeDoc tempTypeDoc : typeDocList) {
//                        if (StringUtils.isEmpty(key)) {
//                            key = tempTypeDoc.getExtColumn() + " like " + "'%" + keywords + "%'";
//                        } else {
//                            key += " or " + tempTypeDoc.getExtColumn() + " like " + "'%" + keywords + "%'";
//                        }
//
//                    }
//                    if (!StringUtils.isEmpty(key))
//                        param.put("fileKeyword", key);
//                }
//
//            }
//            if (!StringUtils.isEmpty(openState)) {
//                param.put("openState", "0");
//            }
//            IPage<ArchivesDoc> info = archivesDocMapper.queryList(page, param); // .selectPage(page, queryWrapper);
//            if (info.getRecords().size() > 0 && ("doc".equals(fileType) || "scattered".equals(fileType))) {
//
//                List<Long> docIds = new ArrayList<Long>();
//                for (ArchivesDoc docE : info.getRecords()) {
//                    docIds.add(docE.getId());
//                }
//                QueryWrapper<ArchivesDocFile> queryWrapperFile = new QueryWrapper<ArchivesDocFile>();
//                queryWrapperFile.in("docId", docIds);
//                List<ArchivesDocFile> selectList = archivesDocFileMapper.selectList(queryWrapperFile);
//                for (int i = 0; i < info.getRecords().size(); i++) {
//                    for (ArchivesDocFile archivesDocFile : selectList) {
//                        if (archivesDocFile.getDocId() != null && archivesDocFile.getDocId().intValue() == info
//                                .getRecords().get(i).getId().intValue()) {
//                            info.getRecords().get(i).setIsFile("1");
//                        }
//                    }
//                }
//
//            }
//            // add by lig 2021-07-29 卷附件判断
//            if (info.getRecords().size() > 0 && ("dossier".equals(fileType))) {
//
//                for (int i = 0; i < info.getRecords().size(); i++) {
////                    List<Long> docIds = new ArrayList<Long>();
////                    for (ArchivesDoc docE : info.getRecords()) {
//
//                    Long dId = info.getRecords().get(i).getDossierId();
//                    if (null == dId) continue;
//                    //是否存在附件
//                    Map<String, Object> param1 = new HashMap<String, Object>();
//                    param1.put("dossierId", dId);
//                    if (archivesDocMapper.countFiles(param1) > 0) {
//                        info.getRecords().get(i).setIsFile("1");
//                    }
//
//                }
//
//
//            }
//            if ("3".equals(state) && info.getRecords().size() > 0) {
//                List<Long> identId = new ArrayList<Long>();
//                for (ArchivesDoc archivesDocTemp : info.getRecords()) {
//                    identId.add(archivesDocTemp.getId());
//                }
//                QueryWrapper<ArchivesDestroyRecord> queryWrapperIdent = new QueryWrapper<ArchivesDestroyRecord>();
//                queryWrapperIdent.in("fileId", identId);
//                queryWrapperIdent.eq("state", "0");
//                List<ArchivesDestroyRecord> selectList = archivesDestroyRecordMapper.selectList(queryWrapperIdent);
//                if (selectList != null && selectList.size() > 0) {
//                    for (ArchivesDestroyRecord archivesDestroyRecord : selectList) {
//                        for (int i = 0; i < info.getRecords().size(); i++) {
//                            if (archivesDestroyRecord.getFileId().intValue() == info.getRecords().get(i).getId().intValue()) {
//                                info.getRecords().get(i).setIdentType(archivesDestroyRecord.getIdentType());
//                                info.getRecords().get(i).setIdentValue(archivesDestroyRecord.getProlongTime());
//                            }
//                        }
//                    }
//                }
//            }
//            PageInfo<ArchivesDoc> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
//                    info.getRecords());
//            List<ArchivesTypeDoc> collect = baseCols.stream().sorted(Comparator.comparing(ArchivesTypeDoc::getSort))
//                    .collect(Collectors.toList());
//
//            resultList.setBaseCol(collect);
//            resultList.setListData(data);
//        } else {
//            return ResultUtil.error("门类异常");
//        }
//
//
//        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", resultList);
//
//    }
    @Override
    public Result<ArchivesList> list(Integer pageNum, Integer pageSize, ArchivesSearch archivesSearch) {

        String archivesTimeout = RedisUtil.getSysConfig(SystemConfigType.ARCHIVES_TIMEOUT);

        ArchivesList resultList = new ArchivesList();

        String fondsId = archivesSearch.getFondsId();
        String typeId = archivesSearch.getTypeId();
        String classId = archivesSearch.getClassId();
        String fileType = archivesSearch.getFileType();
        String state = archivesSearch.getState();

        //获取门类
        ArchivesType typeBean = archivesTypeMapper.selectById(typeId);
        if("0".equals(typeId)){
            typeBean =new ArchivesType();
            typeBean.setId(0L);
        }

        if (null != typeBean) {
            String tempFileType1 = "scattered".equals(fileType) ? "doc" : fileType;

            List<ArchivesTypeDoc> baseCols = archivesService.gainBaseField(tempFileType1, typeBean.getId().toString());
            List<String> query = archivesService.gainBaseQueryField(tempFileType1, typeBean.getId().toString());
            Page<ArchivesDoc> page = new Page<ArchivesDoc>(pageNum == null ? 0 : pageNum,
                    pageSize == null ? 10 : pageSize);

            Map<String, Object> param = new HashMap<String, Object>();
            param.put("cols", query);
            param.put("fondsId", fondsId);
            param.put("typeId", typeId);

            //只有 在线接收 模块才显示档案超时天数的倒计时功能
            if(!"6".equals(state)){
                for(int i=0;i<baseCols.size();i++){
                    if(baseCols.get(i).getExtColumn().equals("remainDay")) baseCols.remove(i);
                }
            }

            //添加档案超时提醒设置的天数
            if(!StringUtils.isEmpty(archivesTimeout) && "6".equals(archivesSearch.getState())){
                param.put("archivesTimeout", archivesTimeout);
            }
            if (!StringUtils.isEmpty(classId)) {
                param.put("classId", classId);
            }
            if (!StringUtils.isEmpty(archivesSearch.getDeptId())) {
                param.put("deptId", archivesSearch.getDeptId());
            }
            if (!StringUtils.isEmpty(archivesSearch.getState()))
                param.put("state", archivesSearch.getState());
            param.put("isDelete", archivesSearch.getIsDelete());
            if (!StringUtils.isEmpty(archivesSearch.getDossierId())) {
                param.put("dossierId", archivesSearch.getDossierId());
            }
            if (!StringUtils.isEmpty(archivesSearch.getDocFrom())) {
                param.put("docFrom", archivesSearch.getDocFrom());
            }
            if (!StringUtils.isEmpty(archivesSearch.getDutyCompany())) {
                param.put("dutyCompany", archivesSearch.getDutyCompany());
            }
            List<String> queryCondition = ArchivesUtil.queryCondition(archivesSearch.getCondition());
            param.put("fileType", fileType);
            param.put("query", queryCondition);
            //排序
            if (!StringUtils.isEmpty(fileType))

                if("0".equals(typeId)){
                    param.put("sort", "endTime");
                }else{
                    //孝义排序  前面增加 分类 classNum
                    if (RedisUtil.isAppoint(VersionsMarkType.VERSIONS_3)) {
                        param.put("sort", "classNum ," + ("dossier".equals(fileType) ? "ABS(dossierNum)" : "ABS(itemNum)"));

                    }if (RedisUtil.isAppoint(VersionsMarkType.VERSIONS_4)) {
                        param.put("sort", "classNum ," + ("dossier".equals(fileType) ? "ABS(dossierNum)" : "ABS(itemNum)")+", endTime");
                    } else {
                        param.put("sort", "dossier".equals(fileType) ? "ABS(dossierNum)" : "ABS(itemNum)");
                    }

                    if("6".equals(state)){
                        if (RedisUtil.isAppoint(VersionsMarkType.VERSIONS_4)) {
                            param.put("sort", "endTime asc");
                        }else{
                            param.put("sort", "createTime asc");
                        }
                    }
                }

            //关键词
            if (!StringUtils.isEmpty(archivesSearch.getKeywords())) {

                List<Long> typeIds = new ArrayList<Long>();
                typeIds.add(typeBean.getId());
                //String key = buildKeywordSql(typeIds, archivesSearch.getKeywords(),"a.");
                List<String> columnList = buildColumn(typeIds);
                if("0".equals(typeId)){
                    //key="a.archivalNum like '%"+archivesSearch.getKeywords()+"%' or a.dossierNum like '%"+archivesSearch.getKeywords()+"%' or a.itemNum like '%"+archivesSearch.getKeywords()+"%' or a.title like '%"+archivesSearch.getKeywords()+"%'";
                    columnList.add("archivalNum");
                    columnList.add("dossierNum");
                    columnList.add("itemNum");
                    columnList.add("title");
                }
                // -----
                //if (!StringUtils.isEmpty(key))
                //    param.put("fileKeyword", key);

                if(!columnList.isEmpty()){
                    param.put("columnList", columnList);
                    param.put("keywords", archivesSearch.getKeywords());
                }

            }
            if (!"0".equals(typeId)&&!StringUtils.isEmpty(archivesSearch.getClassIds())) {
                param.put("classIds", archivesSearch.getClassIds());
            }
            if (!StringUtils.isEmpty(archivesSearch.getSecret())) {
                param.put("secret", archivesSearch.getSecret());
            }
            if (!StringUtils.isEmpty(archivesSearch.getOpenState())) {
                param.put("openState", "0");
            }
            IPage<ArchivesDoc> info =null;

            if("0".equals(typeId)){
                info=archivesDocMapper.queryListReceive(page,param);
            }else{
                info = archivesDocMapper.queryList(page, param); // .selectPage(page, queryWrapper);
            }
            List<ArchivesDoc> records = info.getRecords();

            for (ArchivesDoc record: records ) {

                String classNum = StringUtil.split(record.getClassNum());

                record.setClassNum(classNum);
            }


            if (info.getRecords().size() > 0 && ("doc".equals(fileType) || "scattered".equals(fileType))) {


//                List<Long> docIds = new ArrayList<Long>();
//                for (ArchivesDoc docE : info.getRecords()) {
//                    docIds.add(docE.getId());
//                }
                List<Long> docIds = ArchivesUtil.buildDocIds(info.getRecords());
                QueryWrapper<ArchivesDocFile> queryWrapperFile = new QueryWrapper<ArchivesDocFile>();
                queryWrapperFile.in("docId", docIds);
                List<ArchivesDocFile> selectList = archivesDocFileMapper.selectList(queryWrapperFile);
                for (int i = 0; i < info.getRecords().size(); i++) {
                    for (ArchivesDocFile archivesDocFile : selectList) {
                        if (archivesDocFile.getDocId() != null && archivesDocFile.getDocId().intValue() == info
                                .getRecords().get(i).getId().intValue()) {
                            info.getRecords().get(i).setIsFile("1");
                        }
                    }
                }
            }

            // add by lig 2021-07-29 卷附件判断
            if (info.getRecords().size() > 0 && ("dossier".equals(fileType))) {

                for (int i = 0; i < info.getRecords().size(); i++) {
//                    List<Long> docIds = new ArrayList<Long>();
//                    for (ArchivesDoc docE : info.getRecords()) {

                    Long dId = info.getRecords().get(i).getDossierId();
                    if (null == dId) continue;
                    //是否存在附件
                    Map<String, Object> param1 = new HashMap<String, Object>();
                    param1.put("dossierId", dId);
                    if (archivesDocMapper.queryFiles(param1).size() > 0) {
                        info.getRecords().get(i).setIsFile("1");
                    }

                }
            }
            if ("3".equals(archivesSearch.getState()) && info.getRecords().size() > 0) {
                List<Long> identId = new ArrayList<Long>();
                for (ArchivesDoc archivesDocTemp : info.getRecords()) {
                    identId.add(archivesDocTemp.getId());
                }
                QueryWrapper<ArchivesDestroyRecord> queryWrapperIdent = new QueryWrapper<ArchivesDestroyRecord>();
                queryWrapperIdent.in("fileId", identId);
                queryWrapperIdent.eq("state", "0");
                List<ArchivesDestroyRecord> selectList = archivesDestroyRecordMapper.selectList(queryWrapperIdent);
                if (selectList != null && selectList.size() > 0) {
                    for (ArchivesDestroyRecord archivesDestroyRecord : selectList) {
                        for (int i = 0; i < info.getRecords().size(); i++) {
                            if (archivesDestroyRecord.getFileId().intValue() == info.getRecords().get(i).getId().intValue()) {
                                info.getRecords().get(i).setIdentType(archivesDestroyRecord.getIdentType());
                                info.getRecords().get(i).setIdentValue(archivesDestroyRecord.getProlongTime());
                            }
                        }
                    }
                }
            }
            PageInfo<ArchivesDoc> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
                    info.getRecords());



            List<ArchivesTypeDoc> collect = baseCols.stream().sorted(Comparator.comparing(ArchivesTypeDoc::getSort))
                    .collect(Collectors.toList());

            resultList.setBaseCol(collect);
            resultList.setListData(data);
        } else {
            return ResultUtil.error("门类异常");
        }


        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", resultList);

    }

//    @Override
//    public Result<ArchivesList> list(Integer pageNum, Integer pageSize, String typeId, String fondsId,
//                                     String dossierId, String condition, String classId, String keywords, String fileType, String deptId,
//                                     String state, String isDelete, String openState, String classIds, String secret) {
//        ArchivesList resultList = new ArchivesList();
//
//        //获取门类
//        ArchivesType typeBean = archivesTypeMapper.selectById(typeId);
//        if(null != typeBean){
//            QueryWrapper<ArchivesTypeDoc> col = new QueryWrapper<>();
//            String tempFileType1 = "scattered".equals(fileType) ? "doc" : fileType;
//            col.eq("extType", tempFileType1);
//            col.eq("isList", "1");
//            col.eq("typeId", typeBean.getId());
//            col.select("id", "extColumn", "columnComment", "colsize", "sort", "extType", "dataType", "listLength");
//            List<ArchivesTypeDoc> baseCols = archivesTypeDocMapper.selectList(col);
//            QueryWrapper<ArchivesTypeDoc> queryCol = new QueryWrapper<>();
//            queryCol.eq("extType", tempFileType1);
//            queryCol.eq("isQuery", "1");
//            queryCol.eq("typeId", typeBean.getId());
//            queryCol.select("extColumn");
//            List<ArchivesTypeDoc> queryCols = archivesTypeDocMapper.selectList(queryCol);
//            List<String> query = new ArrayList<>();
//            for (int i = 0; i < queryCols.size(); i++) {
//                query.add(queryCols.get(i).getExtColumn());
//            }
//            Page<ArchivesDoc> page = new Page<ArchivesDoc>(pageNum == null ? 0 : pageNum,
//                    pageSize == null ? 10 : pageSize);
//            Map<String, Object> param = new HashMap<String, Object>();
//            param.put("cols", query);
//            param.put("fondsId", fondsId);
//            param.put("typeId", typeId);
//            if (!StringUtils.isEmpty(classId)) {
//                param.put("classId", classId);
//            }
//            if (!StringUtils.isEmpty(deptId)) {
//                param.put("deptId", deptId);
//            }
//            if (!StringUtils.isEmpty(state))
//                param.put("state", state);
//            param.put("isDelete", isDelete);
//            if (!StringUtils.isEmpty(dossierId)) {
//                param.put("dossierId", dossierId);
//            }
//            List<String> queryCondition = ArchivesUtil.queryCondition(condition);
//
//            param.put("query", queryCondition);
//            if (!StringUtils.isEmpty(fileType)) {
//                //孝义排序  前面增加 分类 classNum
//                if (RedisUtil.isAppoint(VersionsMarkType.VERSIONS_3)) {
//                    param.put("sort", "classNum ," + ("dossier".equals(fileType) ? "dossierNum" : "itemNum"));
//
//                } else {
//                    param.put("sort", "dossier".equals(fileType) ? "dossierNum" : "itemNum");
//                }
//            }
////                param.put("fileType", fileType);
//            if (!StringUtils.isEmpty(keywords)) {
//                QueryWrapper<ArchivesType> queryWrapperTypeC = new QueryWrapper<ArchivesType>();
//                queryWrapperTypeC.eq("fondsId", fondsId);
//                queryWrapperTypeC.eq("typeId", typeId);
//                queryWrapperTypeC.select("id");
//                List<ArchivesType> selectList = archivesTypeMapper.selectList(queryWrapperTypeC);
//                if (selectList != null && selectList.size() > 0) {
//                    QueryWrapper<ArchivesTypeDoc> queryWrapperTypeDoc = new QueryWrapper<ArchivesTypeDoc>();
//                    queryWrapperTypeDoc.eq("typeId", selectList.get(0).getId());
//                    queryWrapperTypeDoc.eq("isList", "1");
//                    List<ArchivesTypeDoc> typeDocList = archivesTypeDocMapper.selectList(queryWrapperTypeDoc);
//                    String key = "";
//                    for (ArchivesTypeDoc tempTypeDoc : typeDocList) {
//                        if (StringUtils.isEmpty(key)) {
//                            key = tempTypeDoc.getExtColumn() + " like " + "'%" + keywords + "%'";
//                        } else {
//                            key += " or " + tempTypeDoc.getExtColumn() + " like " + "'%" + keywords + "%'";
//                        }
//
//                    }
//                    if (!StringUtils.isEmpty(key))
//                        param.put("fileKeyword", key);
//                }
//
//            }
//            if (!StringUtils.isEmpty(classIds)) {
//                param.put("classIds", classIds);
//            }
//            if (!StringUtils.isEmpty(secret)) {
//                param.put("secret", secret);
//            }
//            if (!StringUtils.isEmpty(openState)) {
//                param.put("openState", "0");
//            }
//            if (!StringUtils.isEmpty(fileType)) {
//                param.put("fileType", fileType);
//            }
//
//
//            IPage<ArchivesDoc> info = archivesDocMapper.queryList(page, param); // .selectPage(page, queryWrapper);
//            if (info.getRecords().size() > 0 && ("doc".equals(fileType) || "scattered".equals(fileType))) {
//
//                List<Long> docIds = new ArrayList<Long>();
//                for (ArchivesDoc docE : info.getRecords()) {
//                    docIds.add(docE.getId());
//                }
//                QueryWrapper<ArchivesDocFile> queryWrapperFile = new QueryWrapper<ArchivesDocFile>();
//                queryWrapperFile.in("docId", docIds);
//                List<ArchivesDocFile> selectList = archivesDocFileMapper.selectList(queryWrapperFile);
//                for (int i = 0; i < info.getRecords().size(); i++) {
//                    for (ArchivesDocFile archivesDocFile : selectList) {
//                        if (archivesDocFile.getDocId() != null && archivesDocFile.getDocId().intValue() == info
//                                .getRecords().get(i).getId().intValue()) {
//                            info.getRecords().get(i).setIsFile("1");
//                        }
//                    }
//                }
//
//            }
//            // add by lig 2021-07-29 卷附件判断
//            if (info.getRecords().size() > 0 && ("dossier".equals(fileType))) {
//
//                for (int i = 0; i < info.getRecords().size(); i++) {
////                    List<Long> docIds = new ArrayList<Long>();
////                    for (ArchivesDoc docE : info.getRecords()) {
//
//                    Long dId = info.getRecords().get(i).getDossierId();
//                    if (null == dId) continue;
//                    //是否存在附件
//                    Map<String, Object> param1 = new HashMap<String, Object>();
//                    param1.put("dossierId", dId);
//                    if (archivesDocMapper.countFiles(param1) > 0) {
//                        info.getRecords().get(i).setIsFile("1");
//                    }
//
//                }
//
//
//            }
//            if ("3".equals(state) && info.getRecords().size() > 0) {
//                List<Long> identId = new ArrayList<Long>();
//                for (ArchivesDoc archivesDocTemp : info.getRecords()) {
//                    identId.add(archivesDocTemp.getId());
//                }
//                QueryWrapper<ArchivesDestroyRecord> queryWrapperIdent = new QueryWrapper<ArchivesDestroyRecord>();
//                queryWrapperIdent.in("fileId", identId);
//                queryWrapperIdent.eq("state", "0");
//                List<ArchivesDestroyRecord> selectList = archivesDestroyRecordMapper.selectList(queryWrapperIdent);
//                if (selectList != null && selectList.size() > 0) {
//                    for (ArchivesDestroyRecord archivesDestroyRecord : selectList) {
//                        for (int i = 0; i < info.getRecords().size(); i++) {
//                            if (archivesDestroyRecord.getFileId().intValue() == info.getRecords().get(i).getId().intValue()) {
//                                info.getRecords().get(i).setIdentType(archivesDestroyRecord.getIdentType());
//                                info.getRecords().get(i).setIdentValue(archivesDestroyRecord.getProlongTime());
//                            }
//                        }
//                    }
//                }
//            }
//            PageInfo<ArchivesDoc> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
//                    info.getRecords());
//            List<ArchivesTypeDoc> collect = baseCols.stream().sorted(Comparator.comparing(ArchivesTypeDoc::getSort))
//                    .collect(Collectors.toList());
//
//            resultList.setBaseCol(collect);
//            resultList.setListData(data);
//        }else{
//            return ResultUtil.error("门类异常");
//        }
//
//
//
//
//        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", resultList);
//
//    }


    @Override
    public Result<ArchivesList> useList(Integer pageNum, Integer pageSize, String extType, String fondsId,
                                        String dossierId, String condition, String classNum, String keywords, String fileType, String deptId,
                                        String state, String isDelete, String openState, HttpServletRequest request) {
        ArchivesList resultList = new ArchivesList();
        if (!StringUtils.isEmpty(condition)) {
            System.out.println("----------------------" + condition);
            condition = Base64Decoder.decodeStr(condition, Charset.forName("UTF-8"));
            System.out.println("----------------------" + condition);
        }
        QueryWrapper<ArchivesType> queryWrapperType = new QueryWrapper<ArchivesType>();
        queryWrapperType.eq("fondsId", fondsId);
        queryWrapperType.eq("typeNum", extType);
        List<ArchivesType> list = archivesTypeMapper.selectList(queryWrapperType);

        if (list != null && list.size() > 0) {
            QueryWrapper<ArchivesTypeDoc> col = new QueryWrapper<>();
            col.eq("extType", fileType);
            col.eq("isList", "1");
            col.eq("typeId", list.get(0).getId());
            col.select("id", "extColumn", "columnComment", "colsize", "sort", "extType", "dataType");
            List<ArchivesTypeDoc> baseCols = archivesTypeDocMapper.selectList(col);
            QueryWrapper<ArchivesTypeDoc> queryCol = new QueryWrapper<>();
            queryCol.eq("extType", fileType);
            queryCol.eq("isQuery", "1");
            queryCol.eq("typeId", list.get(0).getId());
            queryCol.select("extColumn");
            List<ArchivesTypeDoc> queryCols = archivesTypeDocMapper.selectList(queryCol);
            List<String> query = new ArrayList<>();
            for (int i = 0; i < queryCols.size(); i++) {
                query.add(queryCols.get(i).getExtColumn());
            }
            Page<ArchivesDoc> page = new Page<ArchivesDoc>(pageNum == null ? 0 : pageNum,
                    pageSize == null ? 10 : pageSize);
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("cols", query);
            param.put("fondsId", fondsId);
            param.put("typeNum", extType);
            if (!StringUtils.isEmpty(classNum)) {
                param.put("classNum", classNum);
            }
            if (!StringUtils.isEmpty(deptId)) {
                param.put("deptId", deptId);
            }
            if (!StringUtils.isEmpty(state))
                param.put("state", state);
            param.put("isDelete", isDelete);
            if ("doc".equals(fileType))
                if (StringUtils.isEmpty(dossierId)) {
                    param.put("dossierId", "1");

                } else {
                    param.put("dossierId", dossierId);
                }
            List<String> queryCondition = new ArrayList<String>();
            if (!StringUtils.isEmpty(condition) && condition.contains(":")) {
                int indexOf = condition.indexOf("aconcata");
                if (indexOf > 0) {
                    String[] split = condition.split("aconcata");
                    for (String string : split) {
                        if (string.indexOf(":") > 0) {
                            String[] split2 = string.split(":");
                            if (split2[0].equals("base")) {
                                queryCondition.add("a." + split2[1]);
                            } else if (split2[0].equals("ext")) {
                                queryCondition.add("b." + split2[1]);
                            }
                        }
                    }
                } else {
                    if (condition.indexOf(":") > 0) {
                        String[] split2 = condition.split(":");
                        if (split2[0].equals("base")) {
                            queryCondition.add("a." + split2[1]);
                        } else if (split2[0].equals("b." + split2[1])) {
                            queryCondition.add(condition);
                        }
                    }
                }
            }

            if (!StringUtils.isEmpty(keywords)) {
                QueryWrapper<ArchivesType> queryWrapperTypeC = new QueryWrapper<ArchivesType>();
                queryWrapperTypeC.eq("fondsId", fondsId);
                queryWrapperTypeC.eq("typeNum", extType);
                queryWrapperTypeC.select("id");
                List<ArchivesType> selectList = archivesTypeMapper.selectList(queryWrapperTypeC);
                if (selectList != null && selectList.size() > 0) {
                    QueryWrapper<ArchivesTypeDoc> queryWrapperTypeDoc = new QueryWrapper<ArchivesTypeDoc>();
                    queryWrapperTypeDoc.eq("typeId", selectList.get(0).getId());
                    queryWrapperTypeDoc.eq("isList", "1");
                    List<ArchivesTypeDoc> typeDocList = archivesTypeDocMapper.selectList(queryWrapperTypeDoc);
                    String key = "";
                    for (ArchivesTypeDoc tempTypeDoc : typeDocList) {
                        if (StringUtils.isEmpty(key)) {
                            key = tempTypeDoc.getExtColumn() + " like " + "'%" + keywords + "%'";
                        } else {
                            key += " or " + tempTypeDoc.getExtColumn() + " like " + "'%" + keywords + "%'";
                        }

                    }
                    if (!StringUtils.isEmpty(key))
                        param.put("fileKeyword", key);
                }

            }

            param.put("fileType", fileType);
            param.put("query", queryCondition);
            if (!StringUtils.isEmpty(fileType))
                param.put("sort", "dossier".equals(fileType) ? "dossierNum" : "itemNum");
            if (!StringUtils.isEmpty(keywords))
                param.put("fileKeyword", "%" + keywords + "%");
            if (!StringUtils.isEmpty(openState)) {
                param.put("openState", "0");
            }
            IPage<ArchivesDoc> info = archivesDocMapper.queryList(page, param); // .selectPage(page, queryWrapper);
            // //.queryList(page,param); //
            List<ArchivesDocResult> archivesDocResults = new ArrayList<ArchivesDocResult>();
            if (info.getRecords().size() > 0) {
                List<Long> ids = new ArrayList<Long>();
                for (ArchivesDoc doc : info.getRecords()) {
                    if ("doc".equals(doc.getFileType()) && "2".equals(doc.getOpenState())) {
                        Long id = doc.getId();
                        ids.add(id);
                    }
                    ArchivesDocResult target = new ArchivesDocResult();
                    BeanUtils.copyProperties(doc, target);
                    archivesDocResults.add(target);
                }
                if (ids.size() > 0) {
                    UserInfo loginInfo = userInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
                    QueryWrapper<UseBorrowRecord> queryWrapperBR = new QueryWrapper<UseBorrowRecord>();
                    // queryWrapperBR.eq("state","2");
                    queryWrapperBR.eq("userId", loginInfo.getId());
                    queryWrapperBR.in("fileId", ids);
                    queryWrapperBR.orderByDesc("createTime");
                    // queryWrapperBR.groupBy("fileId");
                    queryWrapperBR.select("fileId", "state");
                    List<UseBorrowRecord> selectList = borrowRecordMapper.selectList(queryWrapperBR);
                    for (ArchivesDocResult tempResult : archivesDocResults) {
                        for (UseBorrowRecord useBorrowRecord : selectList) {
                            if (tempResult.getId().intValue() == useBorrowRecord.getFileId().intValue()
                                    && StringUtils.isEmpty(tempResult.getIsState())) {
                                tempResult.setIsState(useBorrowRecord.getState());
                            }
                        }
                    }
                }
            }
            PageInfo<ArchivesDocResult> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
                    archivesDocResults);

            List<ArchivesTypeDoc> collect = baseCols.stream().sorted(Comparator.comparing(ArchivesTypeDoc::getSort))
                    .collect(Collectors.toList());
            resultList.setBaseCol(collect);
            resultList.setListData(data);
        }
        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", resultList);
    }

    @Override
    public Result<ArchivesDetails> details(ArchivesDoc archivesDoc) {
        ArchivesDetails archivesDetails = new ArchivesDetails();

        ArchivesType atBean = archivesTypeMapper.selectById(archivesDoc.getTypeId());
        ArchivesClass archivesClass=archivesClassMapper.selectById(archivesDoc.getClassId());
        ArchivesFonds archivesFonds = archivesFondsMapper.selectById(archivesDoc.getFondsId());
        if (null != atBean) {
            //修改 据说之前需求是  散文件使用件的字段配置 lig 2021-07-01
            String fileType = archivesDoc.getFileType().equals("scattered") ? "doc" : archivesDoc.getFileType();

            List<ArchivesTypeDoc> baseCols = archivesService.gainBaseField(fileType, atBean.getId().toString());
            archivesDetails.setBaseCol(baseCols);
            List<String> query = archivesService.gainBaseQueryField(fileType, atBean.getId().toString());
            for(int i=0;i<query.size();i++){
                if("fondsName".equals(query.get(i))) query.remove(i);
            }
            query.add("id");
            query.add("typeId");

            QueryWrapper<ArchivesDoc> queryWrapperDoc = new QueryWrapper<ArchivesDoc>();
            queryWrapperDoc.select(query.toArray(new String[0]));
            queryWrapperDoc.eq("id", archivesDoc.getId());
            ArchivesDoc aDoc = archivesDocMapper.selectOne(queryWrapperDoc);
            if (null != aDoc) {
                aDoc.setTypeNum(atBean.getTypeNum());
                aDoc.setFondsName(archivesFonds.getFondsName());
                /*if (StringUtils.isNotEmpty(aDoc.getClassNum())) {
                    aDoc.setClassNum(atBean.getTypeNum() + RedisUtil.getSysConfig(SystemConfigType.CLASSNUM_WEB).trim() + aDoc.getClassNum());
                }*/
                if(archivesDoc.getClassId()!=null &&archivesDoc.getClassId() !=0){
                    aDoc.setClassNum(atBean.getTypeNum()+RedisUtil.getSysConfig(SystemConfigType.CLASSNUM_WEB).trim()+archivesClass.getTreeStr());
                }
                archivesDetails.setData(aDoc);
            }
        }


        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", archivesDetails);
    }

    @Override
    public Result<ArchivesDetails> useDetails(ArchivesDoc archivesDoc, HttpServletRequest request) {
        UserInfo loginInfo = userInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
        ArchivesDetails archivesDetails = new ArchivesDetails();

        QueryWrapper<ArchivesType> queryWrapperType = ArchivesUtil.builBaseQueryWrapper(new BaseQueryDto(archivesDoc.getFondsId(), archivesDoc.getTypeNum()));

        List<ArchivesType> list = archivesTypeMapper.selectList(queryWrapperType);
        if (list != null && list.size() > 0) {
            String fondsName=null;

            QueryWrapper<ArchivesTypeDoc> col = new QueryWrapper<>();
            col.eq("extType", archivesDoc.getFileType());
            col.eq("isQuery", "1");
            col.eq("typeId", list.get(0).getId());
            List<ArchivesTypeDoc> baseCols = archivesTypeDocMapper.selectList(col);

            QueryWrapper<ArchivesTypeDoc> col1 = new QueryWrapper<>();
            col1.eq("extType", archivesDoc.getFileType());
            col1.eq("isList", "1");
            col1.eq("typeId", list.get(0).getId());
            List<ArchivesTypeDoc> baseCols1 = archivesTypeDocMapper.selectList(col);

            archivesDetails.setBaseCol(baseCols1);
            List<String> query = new ArrayList<>();
            query.add("id");
            for (int i = 0; i < baseCols.size(); i++) {
                String extColumn = baseCols.get(i).getExtColumn();
                if (extColumn.equals("fondsNum") || extColumn.equals("typeNum") || extColumn.equals("classNum")) {
                    query.add(extColumn.replace("Num","Id"));
                }else if (extColumn.equals("fondsName")){
                    fondsName="存在";
                }else{
                    query.add(baseCols.get(i).getExtColumn());
                }
            }
            QueryWrapper<ArchivesDoc> queryWrapperDoc = new QueryWrapper<ArchivesDoc>();
            queryWrapperDoc.select(query.toArray(new String[0]));

            queryWrapperDoc.eq("id", archivesDoc.getId());
            List<ArchivesDoc> selectList = archivesDocMapper.selectList(queryWrapperDoc);
            if (selectList != null && selectList.size() > 0) {
                ArchivesDoc archivesDoc2 = selectList.get(0);
                archivesDoc2.setFondsName(fondsName);
                //构造基础信息
                archivesService.buildBaseInfo(archivesDoc2);
                QueryWrapper<UseBorrowRecord> queryWrapperBR = new QueryWrapper<UseBorrowRecord>();
                queryWrapperBR.eq("fileId", archivesDoc2.getId());
                queryWrapperBR.eq("userId", loginInfo.getId());
                // queryWrapperBR.eq("state","2");
                List<UseBorrowRecord> selectList2 = borrowRecordMapper.selectList(queryWrapperBR);
                if (selectList2 != null && selectList2.size() > 0) {
                    archivesDetails.setIsState(selectList2.get(0).getState());
                }
                archivesDetails.setData(archivesDoc2);
            }
        }
        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", archivesDetails);
    }

    @Override
    public Result<String> docEdit(ArchivesDoc archivesDoc) {
        try {
            Map<String, List<ArchivesTypeDoc>> mapkey = new HashMap<>();
            Map<String, List<ArchivesTypeDoc>> map = new HashMap<>();
            List<ArchivesTypeDoc> typeDocs = archivesTypeDocMapper.selectList(null);
            QueryWrapper<ArchivesType> queryType = new QueryWrapper<ArchivesType>();
            queryType.eq("fondsId", archivesDoc.getFondsId());
            queryType.eq("typeNum", archivesDoc.getTypeNum());
            List<ArchivesType> types = archivesTypeMapper.selectList(queryType);
            for (ArchivesType archivesType : types) {
                mapkey.put(archivesType.getFondsId() + ":" + archivesType.getId(), null);
            }
            for (String key : mapkey.keySet()) {
                List<ArchivesTypeDoc> tempList = new ArrayList<ArchivesTypeDoc>();
                for (ArchivesTypeDoc archivesTypeDoc : typeDocs) {
                    String typeId = archivesTypeDoc.getTypeId().toString();
                    if (key.split(":")[1].equals(typeId)) {
                        tempList.add(archivesTypeDoc);
                    }
                }
                map.put(key.split(":")[0], tempList);
            }

            int rows = archivesDocMapper.updateById(archivesDoc);
            if (rows > 0) {
                return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", "");
            } else {
                return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "操作失败！！！", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "操作失败！！！", "");
        }

    }

    @Override
    public List<Map<String, Object>> printGetDocInfo(List<Long> ids, String typeNum, String fondsId) {
        QueryWrapper<ArchivesBase> queryBaseCol = new QueryWrapper<ArchivesBase>();
        queryBaseCol.eq("extType", "doc");
        List<ArchivesBase> baseCol = archivesBaseMapper.selectList(queryBaseCol);
        QueryWrapper<ArchivesDocExt> queryExtCol = new QueryWrapper<ArchivesDocExt>();
        queryExtCol.eq("extType", typeNum);
        queryExtCol.eq("fondsId", fondsId);
        queryExtCol.eq("fileType", "doc");
        List<String> colList = new ArrayList<String>();
        for (ArchivesBase archivesBase : baseCol) {
            colList.add(archivesBase.getExtColumn());
        }
        List<ArchivesDoc> baseDoc = new ArrayList<ArchivesDoc>();
        List<ArchivesDocValue> docValueList = new ArrayList<ArchivesDocValue>();
        if (ids != null && ids.size() > 0) {
            QueryWrapper<ArchivesDoc> queryWrapperDoc = new QueryWrapper<ArchivesDoc>();
            queryWrapperDoc.select(colList.toArray(new String[0]));
            queryWrapperDoc.in("id", ids);
            baseDoc = archivesDocMapper.selectList(queryWrapperDoc);
            QueryWrapper<ArchivesDocValue> queryExtValue = new QueryWrapper<ArchivesDocValue>();
            queryExtValue.in("docId", ids);
            docValueList = archivesDocValueMapper.selectList(queryExtValue);
        }

        List<Map<String, Object>> map = new ArrayList<Map<String, Object>>();
        for (ArchivesDoc archivesDoc : baseDoc) {
            Map<String, Object> parseObject = (Map<String, Object>) JSON.parseObject(JSON.toJSONString(archivesDoc),
                    Map.class);
            for (ArchivesDocValue archivesDocValue : docValueList) {
                if (archivesDoc.getId().equals(archivesDocValue.getDocId())) {
                    ArchivesDocExt selectById = archivesDocExtMapper.selectById(archivesDocValue.getColumnId());
                    if (selectById != null)
                        parseObject.put(selectById.getExtColumn(), archivesDocValue.getColumnValue());
                }
            }
            map.add(parseObject);
        }
        return map;
    }

    @Override
    public List<Map<String, Object>> printGetDocInfoByDossierId(List<String> ids, String typeNum, String fondsId) {
        QueryWrapper<ArchivesBase> queryBaseCol = new QueryWrapper<ArchivesBase>();
        queryBaseCol.eq("extType", "doc");
        List<ArchivesBase> baseCol = archivesBaseMapper.selectList(queryBaseCol);
        QueryWrapper<ArchivesDocExt> queryExtCol = new QueryWrapper<ArchivesDocExt>();
        queryExtCol.eq("extType", typeNum);
        queryExtCol.eq("fondsId", fondsId);
        queryExtCol.eq("fileType", "doc");
        List<String> colList = new ArrayList<String>();
        for (ArchivesBase archivesBase : baseCol) {
            colList.add(archivesBase.getExtColumn());
        }
        List<ArchivesDoc> baseDoc = new ArrayList<ArchivesDoc>();
        if (ids != null && ids.size() > 0) {
            QueryWrapper<ArchivesDoc> queryWrapperDoc = new QueryWrapper<ArchivesDoc>();
            queryWrapperDoc.select(colList.toArray(new String[0]));
            queryWrapperDoc.in("dossierId", ids);
            baseDoc = archivesDocMapper.selectList(queryWrapperDoc);
        }
        List<Long> baseIds = new ArrayList<Long>();
        if (baseDoc != null && baseDoc.size() > 0) {
            for (ArchivesDoc archivesDoc : baseDoc) {
                baseIds.add(archivesDoc.getId());
            }
        }
        List<ArchivesDocValue> docValueList = new ArrayList<ArchivesDocValue>();
        if (baseIds != null && baseIds.size() > 0) {
            QueryWrapper<ArchivesDocValue> queryExtValue = new QueryWrapper<ArchivesDocValue>();
            queryExtValue.in("docId", baseIds);
            docValueList = archivesDocValueMapper.selectList(queryExtValue);
        }
        List<Map<String, Object>> map = new ArrayList<Map<String, Object>>();
        for (ArchivesDoc archivesDoc : baseDoc) {
            Map<String, Object> parseObject = (Map<String, Object>) JSON.parseObject(JSON.toJSONString(archivesDoc),
                    Map.class);
            for (ArchivesDocValue archivesDocValue : docValueList) {
                if (archivesDocValue.getDocId().equals(archivesDoc.getId().toString())) {
                    ArchivesDocExt selectById = archivesDocExtMapper.selectById(archivesDocValue.getColumnId());
                    if (selectById != null)
                        parseObject.put(selectById.getExtColumn(), archivesDocValue.getColumnValue());
                }
            }
            map.add(parseObject);
        }
        return map;
    }

    @Override
    public Result<PageInfo<ArchivesDocResult>> keywordsList(Integer pageNum, Integer pageSize, String typeId,
                                                            String fondsId, String classId, String keywords, String fileType, String state, String openState, UserInfo loginUser, String secret) {


        Page<ArchivesDoc> page = new Page<ArchivesDoc>(pageNum == null ? 0 : pageNum, pageSize == null ? 10 : pageSize);
        QueryWrapper<ArchivesDoc> queryWrapper = new QueryWrapper<ArchivesDoc>();
        //不展示卷内文件 by lig 2021-12-13
        queryWrapper.eq("dossierId", "0");

        //超级管理员可以看到所有全宗的数据，其它管理员只能看到自己本部门全宗的数据
        String userName= loginUser.getUsername();
        String role = loginUser.getRole();
        if("admin".equals(userName) || "超级管理员".equals(role)){
            if (!StringUtils.isEmpty(fondsId)) {
                queryWrapper.eq("fondsId", fondsId);
            }
        }else{
            fondsId=loginUser.getFondsId();
            queryWrapper.eq(!StringUtils.isEmpty(fondsId),"fondsId", fondsId);
        }


        if (!StringUtils.isEmpty(classId))
            queryWrapper.eq("classId", classId);
        if (!StringUtils.isEmpty(typeId))
            queryWrapper.eq("typeId", typeId);
        if (!StringUtils.isEmpty(fileType))
            queryWrapper.eq("fileType", "0".equals(fileType) ? "doc" : "dossier");
        if (!StringUtils.isEmpty(state))
            queryWrapper.in("state",state);
            //queryWrapper.eq("state", state);
        if (!StringUtils.isEmpty(openState))
            queryWrapper.eq("openState", "0");
        if (!StringUtils.isEmpty(secret))
            queryWrapper.eq("secret", secret);
        queryWrapper.eq("isDelete", "0");

        if (!StringUtils.isEmpty(keywords)) {
            QueryWrapper<ArchivesType> queryWrapperTypeC = new QueryWrapper<ArchivesType>();
            if (!StringUtils.isEmpty(fondsId))
                queryWrapperTypeC.eq("fondsId", fondsId);
            if (!StringUtils.isEmpty(typeId))
                queryWrapperTypeC.eq("id", typeId);
            queryWrapperTypeC.select("id");
            List<ArchivesType> selectList = archivesTypeMapper.selectList(queryWrapperTypeC);
            if (selectList != null && selectList.size() > 0) {
                List<Long> ids = new ArrayList<Long>();
                for (ArchivesType tempType : selectList) {
                    ids.add(tempType.getId());
                }
                //String key = buildKeywordSql(ids, keywords,"");
                //if (!StringUtils.isEmpty(key))
                //    queryWrapper.last("and ( " + key + " )");

                List<String> columnList = buildColumn(ids);
                if(!columnList.isEmpty()){
                    queryWrapper.and(wq ->{
                        for(int i=0;i<columnList.size();i++){
                            String column=columnList.get(i);
                            wq.like(column,keywords);
                            if(i!=columnList.size()-1){
                                wq.or();
                            }
                        }
                    });
                }
            }
        }

        IPage<ArchivesDoc> info = archivesDocMapper.selectPage(page, queryWrapper);
        List<ArchivesDocResult> archivesDocResults = new ArrayList<ArchivesDocResult>();
        if (info.getRecords().size() > 0) {
            List<Long> ids = new ArrayList<Long>();
            for (ArchivesDoc doc : info.getRecords()) {
//                if ("doc".equals(doc.getFileType()) && "2".equals(doc.getOpenState())) {
//                if ("doc".equals(doc.getFileType())) {
//                    Long id = doc.getId();
//                    ids.add(id);
//                }

//                if(doc.getDossierId() == 0){
//                    Long id = doc.getId();
//                    ids.add(id);
//                }
                Long id = doc.getId();
                ids.add(id);

                if(!"6".equals(state)){
                    ArchivesType archivesType = archivesTypeMapper.selectById(doc.getTypeId());
                    if(archivesType!=null){
                        doc.setTypeNum(archivesType.getTypeNum());
                    }


                    ArchivesClass archivesClass = archivesClassMapper.selectById(doc.getClassId());
                    if(archivesClass!=null){
                        doc.setClassTreeStr(archivesClass.getTreeStr());
                    }
                }


                ArchivesDocResult target = new ArchivesDocResult();
                BeanUtils.copyProperties(doc, target);
                archivesDocResults.add(target);
            }
            if (ids.size() > 0) {
                QueryWrapper<UseBorrowRecord> queryWrapperBR = new QueryWrapper<UseBorrowRecord>();
                // queryWrapperBR.eq("state","2");
                queryWrapperBR.eq("userId", loginUser.getId());
                queryWrapperBR.in("fileId", ids);
                queryWrapperBR.orderByDesc("createTime");
                queryWrapperBR.and(Wrapper -> Wrapper.eq("state", "0").or().eq("state", "2"));

                // queryWrapperBR.groupBy("fileId");
                queryWrapperBR.select("fileId", "state");
                List<UseBorrowRecord> selectList = borrowRecordMapper.selectList(queryWrapperBR);
                for (ArchivesDocResult tempResult : archivesDocResults) {
                    for (UseBorrowRecord useBorrowRecord : selectList) {
//                        if (tempResult.getId().intValue() == useBorrowRecord.getFileId().intValue()
//                                && StringUtils.isEmpty(tempResult.getIsState())) {
//                            tempResult.setIsState(useBorrowRecord.getState());
//                        }
                        if (tempResult.getId().intValue() == useBorrowRecord.getFileId().intValue()) {
                            tempResult.setIsState(useBorrowRecord.getState());
                        }
                    }
                }
            }
        }
        PageInfo<ArchivesDocResult> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
                archivesDocResults);

        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", data);
    }

    @Override
    public Result<PageInfo<ArchivesDocResult>> conditionList(Integer pageNum, Integer pageSize, String extType,
                                                             String fondsId, String classNum, String condition, String model, String state, HttpServletRequest request) {
        if (!StringUtils.isEmpty(condition)) {
            Base64 base64 = new Base64();
            try {
                condition = new String(base64.decode(condition), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<String> query = new ArrayList<>();
        query.add("*");
        Page<ArchivesDoc> page = new Page<ArchivesDoc>(pageNum == null ? 0 : pageNum, pageSize == null ? 10 : pageSize);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("cols", query);
        param.put("fondsId", fondsId);
        param.put("typeNum", extType);
        if (!StringUtils.isEmpty(classNum)) {
            param.put("classNum", classNum);
        }
        param.put("state", state);

        List<String> queryCondition = new ArrayList<String>();
        if (!StringUtils.isEmpty(condition) && condition.contains(":")) {
            int indexOf = condition.indexOf("aconcata");
            if (indexOf > 0) {
                String[] split = condition.split("aconcata");
                for (String string : split) {
                    if (string.indexOf(":") > 0) {
                        String[] split2 = string.split(":");
                        if (split2[0].equals("base")) {
                            queryCondition.add("a." + split2[1]);
                        } else if (split2[0].equals("ext")) {
                            queryCondition.add("b." + split2[1]);
                        }
                    }
                }
            } else {
                if (condition.indexOf(":") > 0) {
                    String[] split2 = condition.split(":");
                    if (split2[0].equals("base")) {
                        queryCondition.add("a." + split2[1]);
                    } else if (split2[0].equals("b." + split2[1])) {
                        queryCondition.add(condition);
                    }
                }
            }
        }
        param.put("fileType", model);
        param.put("query", queryCondition);
        param.put("isDelete", "0");
        IPage<ArchivesDoc> info = archivesDocMapper.queryList(page, param); // .selectPage(page, queryWrapper);
        List<ArchivesDocResult> archivesDocResults = new ArrayList<ArchivesDocResult>();
        if (info.getRecords().size() > 0) {
            List<Long> ids = new ArrayList<Long>();
            for (ArchivesDoc doc : info.getRecords()) {
                if ("doc".equals(doc.getFileType()) && "2".equals(doc.getOpenState())) {
                    Long id = doc.getId();
                    ids.add(id);
                }
                ArchivesDocResult target = new ArchivesDocResult();
                BeanUtils.copyProperties(doc, target);
                archivesDocResults.add(target);
            }
            if (ids.size() > 0) {
                QueryWrapper<UseBorrowRecord> queryWrapperBR = new QueryWrapper<UseBorrowRecord>();
                // queryWrapperBR.eq("state","2");
                queryWrapperBR.in("fileId", ids);
                queryWrapperBR.orderByDesc("createTime");
                // queryWrapperBR.groupBy("fileId");
                queryWrapperBR.select("fileId", "state");
                List<UseBorrowRecord> selectList = borrowRecordMapper.selectList(queryWrapperBR);
                for (ArchivesDocResult tempResult : archivesDocResults) {
                    for (UseBorrowRecord useBorrowRecord : selectList) {
                        if (tempResult.getId().intValue() == useBorrowRecord.getFileId().intValue()
                                && StringUtils.isEmpty(tempResult.getIsState())) {
                            tempResult.setIsState(useBorrowRecord.getState());
                        }
                    }
                }
            }
        }
        PageInfo<ArchivesDocResult> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
                archivesDocResults);
        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", data);

    }

    @Override
    public Result<PageInfo<ArchivesDoc>> conditionListManager(Integer pageNum, Integer pageSize, String typeId,
                                                              String fondsId, String classId, String condition, String model, String state) {
        if (!StringUtils.isEmpty(condition)) {
            Base64 base64 = new Base64();
            try {
                condition = new String(base64.decode(condition), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<String> query = new ArrayList<>();
        query.add("*");
        Page<ArchivesDoc> page = new Page<ArchivesDoc>(pageNum == null ? 0 : pageNum, pageSize == null ? 10 : pageSize);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("cols", query);
        param.put("fondsId", fondsId);
        param.put("typeId", typeId);
        if (!StringUtils.isEmpty(classId)) {
            param.put("classId", classId);
        }
        param.put("state", state);

        List<String> queryCondition = new ArrayList<String>();
        if (!StringUtils.isEmpty(condition) && condition.contains(":")) {
            int indexOf = condition.indexOf("aconcata");
            if (indexOf > 0) {
                String[] split = condition.split("aconcata");
                for (String string : split) {
                    if (string.indexOf(":") > 0) {
                        String[] split2 = string.split(":");
                        if (split2[0].equals("base")) {
                            queryCondition.add("a." + split2[1]);
                        } else if (split2[0].equals("ext")) {
                            queryCondition.add("b." + split2[1]);
                        }
                    }
                }
            } else {
                if (condition.indexOf(":") > 0) {
                    String[] split2 = condition.split(":");
                    if (split2[0].equals("base")) {
                        queryCondition.add("a." + split2[1]);
                    } else if (split2[0].equals("b." + split2[1])) {
                        queryCondition.add(condition);
                    }
                }
            }
        }
        if (!StringUtils.isEmpty(model))
            param.put("fileType", model);
        param.put("query", queryCondition);
        param.put("isDelete", "0");
        IPage<ArchivesDoc> info = archivesDocMapper.queryList(page, param); // .selectPage(page, queryWrapper);

        PageInfo<ArchivesDoc> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
                info.getRecords());
        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", data);

    }

    @Override
    public Result<PageInfo<ArchivesDoc>> keywordsListManager(Integer pageNum, Integer pageSize, String typeId,
                                                             String fondsId, String classId, String keywords, String fileType, String state, String openState) {
        Page<ArchivesDoc> page = new Page<ArchivesDoc>(pageNum == null ? 0 : pageNum, pageSize == null ? 10 : pageSize);
        QueryWrapper<ArchivesDoc> queryWrapper = new QueryWrapper<ArchivesDoc>();
        if (!StringUtils.isEmpty(fondsId))
            queryWrapper.eq("fondsId", fondsId);
        if (!StringUtils.isEmpty(classId))
            queryWrapper.eq("classId", classId);
        if (!StringUtils.isEmpty(typeId))
            queryWrapper.eq("typeId", typeId);
        if (!StringUtils.isEmpty(fileType))
            queryWrapper.eq("fileType", "0".equals(fileType) ? "doc" : "dossier");
        if (!StringUtils.isEmpty(state))
            queryWrapper.eq("state", state);
        if (!StringUtils.isEmpty(openState))
            queryWrapper.eq("openState", openState);
        queryWrapper.eq("isDelete", "0");

        if (!StringUtils.isEmpty(keywords)) {
            QueryWrapper<ArchivesType> queryWrapperTypeC = new QueryWrapper<ArchivesType>();
            if (!StringUtils.isEmpty(fondsId))
                queryWrapperTypeC.eq("fondsId", fondsId);
            if (!StringUtils.isEmpty(typeId))
                queryWrapperTypeC.eq("id", typeId);
            queryWrapperTypeC.select("id");
            List<ArchivesType> selectList = archivesTypeMapper.selectList(queryWrapperTypeC);
            if (selectList != null && selectList.size() > 0) {
                List<Long> ids = new ArrayList<Long>();
                for (ArchivesType tempType : selectList) {
                    ids.add(tempType.getId());
                }
                //String key = buildKeywordSql(ids,keywords,"");
                //if (!StringUtils.isEmpty(key))
                //    queryWrapper.last("and ( " + key + " )");

                List<String> columnList = buildColumn(ids);
                if(!columnList.isEmpty()){
                    queryWrapper.and(wq ->{
                        for(int i=0;i<columnList.size();i++){
                            String column=columnList.get(i);
                            wq.like(column,keywords);
                            if(i!=columnList.size()-1){
                                wq.or();
                            }
                        }
                    });
                }


            }
        }

        IPage<ArchivesDoc> info = archivesDocMapper.selectPage(page, queryWrapper);

        if (info.getRecords().size() > 0) {
            List<Long> ids = new ArrayList<Long>();
            for (ArchivesDoc doc : info.getRecords()) {
                Long id = doc.getId();
                ids.add(id);

                ArchivesType archivesType = archivesTypeMapper.selectById(doc.getTypeId());
                if(archivesType!=null){
                    doc.setTypeNum(archivesType.getTypeNum());
                }


                ArchivesClass archivesClass = archivesClassMapper.selectById(doc.getClassId());
                if(archivesClass!=null){
                    doc.setClassTreeStr(archivesClass.getTreeStr());
                }

                ArchivesFonds archivesFonds=archivesFondsMapper.selectById(doc.getFondsId());
                if(archivesFonds!=null){
                    doc.setFondsNum(archivesFonds.getFondsNum());
                    doc.setFondsName(archivesFonds.getFondsName());
                }

            }
        }


        PageInfo<ArchivesDoc> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
                info.getRecords());

        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", data);
    }


    @Override
    public Result<PageInfo<ArchivesDoc>> keywordsTypesListManager(Integer pageNum, Integer pageSize, String typeName,
                                                                  String keywords, String fileType, String state, String openState) {
        Page<ArchivesDoc> page = new Page<ArchivesDoc>(pageNum == null ? 0 : pageNum, pageSize == null ? 10 : pageSize);

        List<String> typeIds = null;
        List<String> fondsIds = null;
        if (!StringUtils.isEmpty(typeName)) {
            QueryWrapper<ArchivesType> qw = new QueryWrapper<ArchivesType>();
//            qw.select("typeNum");
            qw.eq("typeName", typeName);
            List<ArchivesType> selectList = archivesTypeMapper.selectList(qw);
            if (selectList != null && selectList.size() > 0) {
                fondsIds = buildfondsIds(selectList);
                typeIds = buildTypes(selectList);
            }
        }


        QueryWrapper<ArchivesDoc> queryWrapper = new QueryWrapper<ArchivesDoc>();
        if (!StringUtils.isEmpty(fileType))
            queryWrapper.eq("fileType", "0".equals(fileType) ? "doc" : "dossier");
        if (!StringUtils.isEmpty(state))
            queryWrapper.eq("state", state);
        if (!StringUtils.isEmpty(openState))
            queryWrapper.eq("openState", "0");
        if (null != typeIds && typeIds.size() > 0)
            queryWrapper.in("typeId", typeIds);
        if (null != fondsIds && fondsIds.size() > 0)
            queryWrapper.in("fondsId", fondsIds);
        queryWrapper.eq("isDelete", "0");

        if (!StringUtils.isEmpty(keywords)) {
            QueryWrapper<ArchivesType> queryWrapperTypeC = new QueryWrapper<ArchivesType>();
            if (null != typeIds && typeIds.size() > 0)
                queryWrapperTypeC.in("id", typeIds);
            if (null != fondsIds && fondsIds.size() > 0)
                queryWrapperTypeC.in("fondsId", fondsIds);
            queryWrapperTypeC.select("id");
            List<ArchivesType> selectList = archivesTypeMapper.selectList(queryWrapperTypeC);
            if (selectList != null && selectList.size() > 0) {
                List<Long> ids = new ArrayList<Long>();
                for (ArchivesType tempType : selectList) {
                    ids.add(tempType.getId());
                }
                //String key = buildKeywordSql(ids,keywords,"");
                //if (!StringUtils.isEmpty(key))
                //    queryWrapper.last("and ( " + key + " )");

                List<String> columnList = buildColumn(ids);
                if(!columnList.isEmpty()){
                    queryWrapper.and(wq ->{
                        for(int i=0;i<columnList.size();i++){
                            String column=columnList.get(i);
                            wq.like(column,keywords);
                            if(i!=columnList.size()-1){
                                wq.or();
                            }
                        }
                    });
                }
            }
        }


        IPage<ArchivesDoc> info = archivesDocMapper.selectPage(page, queryWrapper);
        if (info.getRecords().size() > 0) {
            List<Long> ids = new ArrayList<Long>();
            for (ArchivesDoc doc : info.getRecords()) {
                Long id = doc.getId();
                ids.add(id);

                ArchivesType archivesType = archivesTypeMapper.selectById(doc.getTypeId());
                if(archivesType!=null){
                    doc.setTypeNum(archivesType.getTypeNum());
                }


                ArchivesClass archivesClass = archivesClassMapper.selectById(doc.getClassId());
                if(archivesClass!=null){
                    doc.setClassTreeStr(archivesClass.getTreeStr());
                }

                ArchivesFonds archivesFonds=archivesFondsMapper.selectById(doc.getFondsId());
                if(archivesFonds!=null){
                    doc.setFondsNum(archivesFonds.getFondsNum());
                    doc.setFondsName(archivesFonds.getFondsName());
                }

            }
        }

        PageInfo<ArchivesDoc> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
                info.getRecords());

        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", data);
    }


    private List<String> buildTypes(List<ArchivesType> selectList) {
        List<String> typeIds = new ArrayList<String>();
        if (selectList != null && selectList.size() > 0) {
            for (ArchivesType at : selectList) typeIds.add(at.getId().toString());
        }
        return typeIds;
    }

    private List<String> buildfondsIds(List<ArchivesType> selectList) {
        List<String> ids = new ArrayList<String>();
        if (selectList != null && selectList.size() > 0) {
            for (ArchivesType at : selectList) {
//                QueryWrapper<ArchivesFonds> queryWrapper = new QueryWrapper<ArchivesFonds>();
//                queryWrapper.eq("fondsId",at.getfondsId());
//                ArchivesFonds af = archivesFondsMapper.selectOne(queryWrapper);
//                ids.add(af.getId());
                ids.add(at.getFondsId());
            }
        }
        return ids;
    }

    @Override
    public List<ArchivesType> types(UserInfo loginUser) {

        Classifys c = userService.buildUserClassifys(loginUser);
        List<Long> categoryIds = c.getCategoryIds();//门类ids

        List<ArchivesType> list = new ArrayList<ArchivesType>();
        QueryWrapper<ArchivesType> queryWrapper = new QueryWrapper<ArchivesType>();
        queryWrapper.select("id", "fondsId", "typeName", "typeNum", "sort");

        //超级管理员可以看到所有门类的数据，其它管理员只能看到自己本部门门类的数据
        String userName= loginUser.getUsername();
        String role = loginUser.getRole();
        if(!"admin".equals(userName) && !"超级管理员".equals(role)){
            String fondsId=loginUser.getFondsId();
            queryWrapper.eq(StringUtils.isNotEmpty(fondsId),"fondsId",fondsId);
            queryWrapper.in(categoryIds!=null && categoryIds.size()>0,"id",categoryIds);
        }

        //queryWrapper.groupBy("typeName");
        List<ArchivesType> selectList = archivesTypeMapper.selectList(queryWrapper);

        return selectList;
    }


    /**
     * @description: 构造快捷键sql
     *
     * @author: lig
     * @date: 2022/3/18
     */
    private String buildKeywordSql(List<Long> typeIds, String keywords,String tableAlias) {
        QueryWrapper<ArchivesTypeDoc> queryWrapperTypeDoc = new QueryWrapper<ArchivesTypeDoc>();
        queryWrapperTypeDoc.in("typeId", typeIds);
        queryWrapperTypeDoc.eq("isList", "1");
        //queryWrapperTypeDoc.groupBy("extColumn");
        List<ArchivesTypeDoc> typeDocList = archivesTypeDocMapper.selectList(queryWrapperTypeDoc);

        String key = "";
        for (ArchivesTypeDoc tempTypeDoc : typeDocList) {

            //排除基本结构的号
            String extColumn = tempTypeDoc.getExtColumn();
            if (extColumn.equals("typeNum") || extColumn.equals("classNum") || extColumn.equals("fondsName") || extColumn.equals("fondsNum") || extColumn.equals("className"))
                continue;

            if (StringUtils.isEmpty(key)) {
                key = tableAlias + tempTypeDoc.getExtColumn() + " like " + "'%" + keywords + "%'";
            } else {
                key += " or " + tableAlias + tempTypeDoc.getExtColumn() + " like " + "'%" + keywords + "%'";
            }

        }
        return key;
    }

    /**
     * @description: 构造列
     *
     * @author: lig
     * @date: 2022/3/18
     */
    private List<String> buildColumn(List<Long> typeIds) {
        QueryWrapper<ArchivesTypeDoc> queryWrapperTypeDoc = new QueryWrapper<ArchivesTypeDoc>();
        queryWrapperTypeDoc.in("typeId", typeIds);
        queryWrapperTypeDoc.eq("isList", "1");
        //queryWrapperTypeDoc.groupBy("extColumn");
        List<ArchivesTypeDoc> typeDocList = archivesTypeDocMapper.selectList(queryWrapperTypeDoc);

        List<String> columnList=new ArrayList<>();
        for (ArchivesTypeDoc typeDoc:typeDocList){
            String extColumn = typeDoc.getExtColumn();
            if (extColumn.equals("typeNum") || extColumn.equals("classNum") || extColumn.equals("fondsName") || extColumn.equals("fondsNum") || extColumn.equals("className"))
                continue;
            columnList.add(extColumn);
        }
        if(!columnList.isEmpty()) columnList.remove("remainDay");
        return columnList;
    }



}
